package com.yhyvr.console.service;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lz.kit.LogKit;
import com.lz.mybatis.jdbc.auto.dao.SysUserRoleMapper;
import com.lz.mybatis.jdbc.auto.model.SysUserRole;
import com.lz.mybatis.jdbc.auto.model.SysUserRoleExample;
import com.lz.tool.LzStringUtils;
import com.lz.tool.UUIDTool;
import com.yhyvr.console.controller.ControllerQueryTool;
import com.yhyvr.pojo.req.console.BootStrapGridReq;
import com.yhyvr.pojo.resp.CommonResp;
import com.yhyvr.pojo.resp.HttpResult;
import com.yhyvr.pojo.resp.console.BootStrapGridResp;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import javax.annotation.Resource;
import java.util.List;

/**
* 
* @author 
* @create 
**/
 @Service
public class SysUserRoleService{
	@Resource
	private SysUserRoleMapper sysUserRoleMapper;

	public String add(){
		return "console/sysUserRole/sysUserRole_form";
	}

	public String edit(ModelMap modelMap,int id){
			modelMap.put("sysUserRole", sysUserRoleMapper.selectByPrimaryKey(id));
		return "console/sysUserRole/sysUserRole_form";
	}

	public String list(){
		return "console/sysUserRole/sysUserRole_table";
	}

	public BootStrapGridResp json(BootStrapGridReq bootGridReq){
		Page page = PageHelper.offsetPage(bootGridReq.getOffset(), bootGridReq.getLimit());
    	if(bootGridReq.getSort()!=null) {
    		page.setOrderBy(LzStringUtils.chageStringUpCaseAnd_(bootGridReq.getSort()) + " " + bootGridReq.getOrder());
    	}
		SysUserRoleExample sysUserRoleExample = new SysUserRoleExample();
        ControllerQueryTool.setSysUserRoleCriteria(bootGridReq.getQuery(),sysUserRoleExample.createCriteria());
		List<SysUserRole> sysUserRoleList = sysUserRoleMapper.selectByExample(sysUserRoleExample);
		return new BootStrapGridResp(page.getTotal(),sysUserRoleList);
	}

	public HttpResult save(String formObjectJson){
		try{
		SysUserRole sysUserRole= JSON.parseObject(formObjectJson,SysUserRole.class);
			Integer id=sysUserRole.getSnrId();
		if (id!=null) {
			sysUserRoleMapper.updateByPrimaryKeySelective(sysUserRole);
			return CommonResp.getSuccess();
		}else{
			sysUserRoleMapper.insertSelective(sysUserRole);
			return CommonResp.getSuccess();
		}
		}catch(Exception e){
			LogKit.error(e.getMessage(),e);
			return CommonResp.getError();
		}

	}

	public HttpResult delete(String ids){
		String[]idsa=ids.split(",");
		for (String id : idsa) {
		sysUserRoleMapper.deleteByPrimaryKey(Integer.valueOf(id));
		}
		return CommonResp.getSuccess();
	}
}
